require(PBSadmb) # tool for reading admb output (R_report in code)
runs=read.table("clipboard",header=T)
# package that does error bars easily...
require(plotrix)
source("/home/rhome/common.r")
setwd("~/_mymods/crab/crab-model/")
source("~/_mymods/crab/crab-model/R/tools.r")
cm1=readList("cm_r.rep")
cmfit=read.fit("cm") # Separate utility to read in par, cor, std files etc

# Read in results from a model run
setwd("~/_mymods/crab/crab-model/R")

# display names
names(cm1)
names(cmfit)
  # Routines for plotting stage-composition data
  source("r/stagefits.r")
  cat(runs[,2],file="cm.ctl")
  source("/home/rhome/common.r")
  tab1=cbind(cm1$N)
  colnames(tab1)<-c('New crabs', 'Pre-recruits', 'Recruits')
  rownames(tab1)<-cm1$yrs_pf
  xtab(tab1,caption="Table 1.  Numbers at stage estimated from the model.",dec=c(0,1,1,1),cornername='Year', file="d:/t.html",width=.5)
  tab2=cbind(1:6,cm1$L)
  rownames(tab2)<-c("Avg. wt in catch","Trawl survey", "Pot survey","Trawl survey composition","Pot survey composition","Fishery composition")
  xtab(tab2,caption="Table 2.  Likelihood components.",dec=c(0,3),cornername='Component', file="d:/t2.html",width=.6)
  for (i in 1:5)
  {
    cat(runs[,i],file="cm.ctl")
    system("cm -nox")
    nam<-paste("cm",i,sep="")
    tmp=readList("cm_r.rep")
    assign(nam,tmp)
  }
for (i in 1:5)
{
  nam<-paste("cm",i,sep="")
  print(nam)
  cm.figs(dat=nam)
}
cm.figs(cm1)
cm.figs(cm2)
cm.figs(cm3)
cm.figs(cm4)
cm.figs(cm5)

for (i in 1:5) print(get(paste("cm",i,sep=""))$M98)
detach(dat)
rm(dat)

dev.off()

  cm1$yrs_pf
  tab3=cbind(cm1$L,cm2$L,cm3$L,cm4$L,cm5$L)
  colnames(tab3)=c("Model 1","Model 2","Model 3","Model 4","Model 5")
  rownames(tab3)<-c("Avg. wt in catch","Trawl survey", "Pot survey","Trawl survey composition","Pot survey composition","Fishery composition")
  xtab(tab3,caption="Table 3.  Likelihood components.",dec=c(3,3,3,3,3),cornername='Component', file="d:/t3.html",width=.6)
?matrix
tab4=matrix(nrow=35,ncol=5)
tab4[1:34,]=cbind(cm1$recs,cm2$recs,cm3$recs,cm4$recs,cm5$recs)
dim(tab4)
tab4[35,]=0
tab4[35,]=c(mean(cm1$recs),mean(cm2$recs),mean(cm3$recs),mean(cm4$recs),mean(cm5$recs))
colnames(tab4)=c("Model 1","Model 2","Model 3","Model 4","Model 5")
rownames(tab4)<-c(cm1$yrs_pf,"Mean")
rownames(tab4[35:35,])<-"Mean"
rownames(tab4)
xtab(tab4,caption="Table 4.  Recruitment estimates.",dec=c(0,0,0,0,0),cornername='Year', file="d:/t4.html",width=.6)
tab4

c("Avg. wt in catch","Trawl survey", "Pot survey","Trawl survey composition","Pot survey composition","Fishery composition")

dim(cm1$N)

  
